﻿using System.Collections.Generic;
using System.Management;
using Nexus.IPConfiguration.Management.Wrappers;

namespace Nexus.IPConfiguration.Management
{
    /// <summary>
    /// Network adapter factory
    /// </summary>
    public class NetworkAdapterFactory
    {
        #region methods
        /// <summary>
        /// Gets list of network adapters captions
        /// </summary>
        /// <param name="ipOnly">Indicates if only IP-enabled network adapter should be returned</param>
        /// <returns>List of network adapters captions</returns>
        public List<string> GetNetworkAdaptersCaptions(bool ipOnly = true)
        {
            List<string> captions = new List<string>();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var instances = mc.GetInstances();

            foreach (var instance in instances)
            {
                if ((bool)instance["ipEnabled"] == false && ipOnly)
                {
                    continue;
                }

                captions.Add(instance["Caption"] as string);
            }
            mc.Dispose();
            return captions;
        }

        /// <summary>
        /// Gets list of network adapters setting IDs
        /// </summary>
        /// <param name="ipOnly">Indicates if only IP-enabled network adapter should be returned</param>
        /// <returns>List of network adapters setting IDs</returns>
        public List<string> GetNetworkAdaptersSettingIDs(bool ipOnly = true)
        {
            List<string> settingIDs = new List<string>();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            foreach (var instance in mc.GetInstances())
            {
                if ((bool)instance["ipEnabled"] == false && ipOnly)
                {
                    continue;
                }

                settingIDs.Add(instance["SettingID"] as string);
            }
            mc.Dispose();
            return settingIDs;
        }

        /// <summary>
        /// Gets list of network adapters
        /// </summary>
        /// <param name="ipOnly">Indicates if only IP-enabled network adapter should be returned</param>
        /// <returns>List of network adapters</returns>
        public List<NetworkAdapter> GetNetworkAdapters(bool ipOnly = true)
        {
            List<NetworkAdapter> adapters = new List<NetworkAdapter>();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            foreach (var instance in mc.GetInstances())
            {
                if ((bool)instance["ipEnabled"] == false && ipOnly)
                {
                    continue;
                }
                try
                {
                    adapters.Add(new NetworkAdapter(instance["Caption"] as string));
                }
                catch
                {

                }
            }
            mc.Dispose();
            return adapters;
        }

        /// <summary>
        /// Gets list of network adapters' captions and setting IDs 
        /// </summary>
        /// <param name="ipOnly">Indicates if only IP-enabled network adapter should be returned</param>
        /// <returns>List of network adapters' captions and setting ids </returns>
        public List<CaptionAndSettingID> GetCaptionsAndSettingIDs(bool ipOnly = true)
        {
            List<CaptionAndSettingID> adapters = new List<CaptionAndSettingID>();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            foreach (var instance in mc.GetInstances())
            {
                if ((bool)instance["ipEnabled"] == false && ipOnly)
                {
                    continue;
                }
                CaptionAndSettingID id = new CaptionAndSettingID()
                {
                    Caption = instance["Caption"] as string,
                    SettingID = instance["SettingID"] as string
                };
                adapters.Add(id);
            }
            mc.Dispose();
            return adapters;
        }

        /// <summary>
        /// Gets network adapter
        /// </summary>
        /// <param name="param">Network adapter caption or setting ID</param>
        /// <param name="isSettingID">Indicates if first parameter is network adapter's caption or setting ID</param>
        /// <param name="ipOnly">List of network adapters' captions and setting ids</param>
        /// <returns>Network adapter instance</returns>
        public NetworkAdapter GetNetworkAdapter(string param, bool isSettingID = false, bool ipOnly = true)
        {
            if (isSettingID)
            {
                return NetworkAdapter.CreateFromSettingID(param, ipOnly);
            }
            else
            {
                return new NetworkAdapter(param, ipOnly);
            }
        }
        #endregion
    }
}
